<?php

class EntityDefinition extends DomObjDefinition implements IEntityDefinition
{

  /**
   *
   * @var array
   */
  //protected $fields = array();

  /**
   *
   * @var mixed
   */
  protected $pk;

  protected $tables = array();

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IDomObjDefinition#factory()
   * @return IEntity
   */
  public function factory()
  {
    $class = $this->class;

    if (! class_exists($class)) {
      return new CustomEntity($class, $this);
    }

    return parent::factory();
  }

  public function factoryObject($type, $name)
  {
    if ($type != 'field') {
      return parent::factoryObject($type, $name);
    }

    $def = $this->getDefinition('fields', $name);

    return $this->addField($name, FieldDefinition::factory($def));
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IEntityDefinition#addField($name, $def)
   */
  public function addField($name, IFieldDefinition $def)
  {
    if ($this->hasField($name)) {
      throw new DomainException("Duplicate field definition: <b>$name</b>");
    }

    //$this->fields[$name] = $def;
    $this->definitions['fields'][strtolower($name)] = $def;

    if ($def->getPrimaryKey()) {
      $this->pk = $name;
    }

    $table = $def->getTable();
    if (! empty($table) && ! in_array($table, $this->tables)) {
      $this->tables[] = $table;
    }

    return $this;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IEntityDefinition#getTable()
   */
  public function getTable()
  {
    $table = $this->getPkField()->getTable();

    if (empty($table)) {
      $table = Inflector::tableize($this->getName());
    }

    return $table;
  }

  public function getTables()
  {
    return $this->tables;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IEntityDefinition#getField($name)
   */
  public function getField($name)
  {
    if (! $this->hasField($name)) {
      $this->factoryObject('field', $name);
      //throw new FieldNotFoundException("Field not found in entity: <b>$name</b>");
    }

    //return $this->fields[$name];
    return $this->definitions['fields'][$name];
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IEntityDefinition#getFields()
   */
  public function getFields()
  {
    /*if (! empty($this->definitions['fields'])) {
      foreach ($this->definitions['fields'] as $name => $def);
      $this->factoryObject('field', $name);
    }*/

    foreach ($this->definitions['fields'] as $name => $def) {
      if (is_array($def)) {
        $this->factoryObject('field', $name);
      }
    }

    //return $this->fields;
    return $this->definitions['fields'];
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IEntityDefinition#getPk()
   */
  public function getPk()
  {
    if (empty($this->pk)) {
      $this->getFields();
    }

    return $this->pk;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IEntityDefinition#getPk()
   */
  /*public function getPkName()
  {
    $pkName = $this->getPkField()->getName();

    if (empty($pkName)) {
      $pkName = $this->getPk();
    }

    return $pkName;
  }*/

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IEntityDefinition#getPkField()
   */
  public function getPkField()
  {
    return $this->getField($this->getPk());
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IEntityDefinition#hasField($name)
   */
  public function hasField($name)
  {
    //return (isset($this->fields[$name]) && $this->fields[$name] instanceof FieldDefinition);
    return (isset($this->definitions['fields'][strtolower($name)]) && $this->definitions['fields'][strtolower($name)] instanceof FieldDefinition);
  }

}

?>